from django.shortcuts import render
from django.db.models import Count
from django.http import JsonResponse
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework import filters
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from .serializers import *
from .models import *


class MessageFilter(FilterSet):
    class Meta:
        model = Message
        fields = {
            'msgtype': ['exact'],
            # 'site': ['exact'],
            # 'site__name': ['exact'],
            # 'is_discard': ['exact'],
            # 'type': ['exact', 'in']
        }

class MessageViewSet(ModelViewSet):
    queryset = Message.objects.filter()
    serializer_class = MessageSerializers
    filter_backends = (DjangoFilterBackend, filters.OrderingFilter, filters.SearchFilter)
    filter_class = MessageFilter


    # @action(methods=['get'], detail=False)
    # def hk_open_hls(self, request):
    #     """
    #     获取海康智慧工地视频流(短链接,每次点播都要用返回流)
    #     :param request: cameraIndexCode:监控点位编码
    #     :return: url(hls视频流)
    #     """
    #     from Camera.camera import HKOpenApi
    #     cameraIndexCode = request.query_params.get("cameraIndexCode")
    #     if not cameraIndexCode:
    #         return JsonResponse({"code": -1, "msg": 'cameraIndexCode参数为空', 'url': ''}, status=400)
    #     hk = HKOpenApi()
    #     try:
    #         url = hk.get_hls(cameraIndexCode)
    #         if url:
    #             return JsonResponse({"code": 0, "msg": '获取url成功', 'url': url})
    #         return JsonResponse({"code": -1, "msg": '获取url失败', 'url': url})
    #     except Exception as e:
    #         return JsonResponse({"code": -1, "msg": '获取url失败:{}'.format(e), 'url': ""})

    
